#!/bin/bash

# -ffreestanding不使用标准库 -c 仅编译和汇编，不连接
i386-elf-gcc -ffreestanding -c -o function.o function.c

# 反汇编
i386-elf-objdump -d function.o


i386-elf-ld -o function.bin -Ttext 0x0 --oformat binary function.o

ndisasm -b 32 function.bin

# 只有0-9字节是自己写的代码 其他的是垃圾填充 下面的命令可以看段的信息
i386-elf-objdump -h function.o







i386-elf-gcc -ffreestanding -c -o pointers.o pointers.c
i386-elf-ld -o pointers.bin -Ttext 0x0 --oformat binary pointers.o
ndisasm -b 32 pointers.bin
i386-elf-objdump -dh pointers.o


# 00000000  55                push ebp
# 00000001  89E5              mov ebp,esp
# 00000003  83EC10            sub esp,byte +0x10             16字节的局部变量空间
# 00000006  C745FC0F000000    mov dword [ebp-0x4],0xf        将局部变量char* str设置为15,即字符串的内存地址
# 0000000D  C9                leave
# 0000000E  C3                ret



i386-elf-gcc -ffreestanding -c -o localvars.o localvars.c
i386-elf-ld -o localvars.bin -Ttext 0x0 --oformat binary localvars.o
ndisasm -b 32 localvars.bin
i386-elf-objdump -dh localvars.o

i386-elf-gcc -ffreestanding -c -o functioncalls.o functioncalls.c
i386-elf-ld -o functioncalls.bin -Ttext 0x0 --oformat binary functioncalls.o
ndisasm -b 32 functioncalls.bin
i386-elf-objdump -dh functioncalls.o


i386-elf-ld -o functioncalls.bin1 -Ttext 0x10 --oformat binary functioncalls.o
ndisasm -b 32 functioncalls.bin1
